package fi.piet.ec2.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.xerox.amazonws.sdb.Domain;
import com.xerox.amazonws.sdb.Item;
import com.xerox.amazonws.sdb.ItemAttribute;
import com.xerox.amazonws.sdb.QueryResult;
import com.xerox.amazonws.sdb.SDBException;
import com.xerox.amazonws.sdb.SimpleDB;

import fi.piet.ec2.dao.DepartmentDao;
import fi.piet.ec2.department.Department;

public class DepartmentDaoSDB implements DepartmentDao {

	private String awsAccessId;
	private String awsSecretKey;
	
	public Department findDepartment(String departmentId) {
		
		SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey);

		try {
			Domain depDomain = sdb.getDomain("departments");
			Item item = depDomain.getItem(departmentId);
			Department dep = createDepartment(item);
			dep.setId(departmentId);
			return dep;
			
		} catch (SDBException ex) {
			System.err.println("message : " + ex.getMessage());
			System.err.println("requestID : " + ex.getRequestId());
		}
		return null;
	}

	public List<Department> getAllDepartments() {
		SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey);
		Domain depDomain;

		List<Item> items = null;
		List<Department> depList = new ArrayList<Department>();

		QueryResult result = null;
		
		try {
			depDomain = sdb.getDomain("departments");
			result = depDomain.listItems();
			items = result.getItemList();
			
			for (Item item : items) {
				Department dep = createDepartment(item);
				dep.setId(item.getIdentifier());
				depList.add(dep);
			}
			
		} catch (SDBException ex) {
			System.err.println("message : "+ex.getMessage());
			System.err.println("requestID : "+ex.getRequestId());
		}

		return depList;
	}
	
	public void storeDepartment(Department department) {	
		
		String uuId = department.getId();
		if (uuId == null) {
			uuId = java.util.UUID.randomUUID().toString();
		}
		
		SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey);
		Item depItem;
		List<ItemAttribute> attributes;
		Domain departmentDomain;
		try {
			departmentDomain = sdb.getDomain("departments");
			
			depItem = departmentDomain.getItem(uuId);
			attributes = new ArrayList<ItemAttribute>();
			
			attributes.add(new ItemAttribute("name", department.getName(), true));
			attributes.add(new ItemAttribute("description", department.getDescription(), true));
			
			depItem.putAttributes(attributes);
				
		} catch (SDBException ex) {
			System.err.println("message : "+ex.getMessage());
			System.err.println("requestID : "+ex.getRequestId());
		}	
		
	}

	private Department createDepartment(Item item) throws SDBException {
		return createDepartment( item.getAttributes());
	}
	
	private Department createDepartment(List<ItemAttribute> attList) throws SDBException {
		Department result = new Department();
		for(ItemAttribute att : attList) {
			String name = att.getName();
			String value = att.getValue();
			
			if("description".equals(name)) {
				result.setDescription(value);
			} else if ("name".equals(name)) {
				result.setName(value);
			}
		}
		return result;
	}
	
	public void setAwsAccessId(String value) {
		this.awsAccessId = value;
	}
	
	public void setAwsSecretKey(String value) {
		this.awsSecretKey = value;
	}
}
